/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.action;


import com.jspx.boot.environment.Environment;
import com.jspx.boot.sign.LanguageRes;
import com.jspx.txweb.annotation.HttpMethod;
import com.jspx.txweb.table.Member;
import com.jspx.txweb.table.MemberTree;
import com.jspx.txweb.view.TreeView;
import com.jspx.txweb.IUserSession;
import com.jspx.utils.ArrayUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jspx.txweb.annotation.Operate;
import com.jspx.utils.StringUtil;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 2008-9-24
 * Time: 15:23:06
 */
@HttpMethod(caption = "用户树")
public class MemberTreeManageAction extends TreeView {
    private static final Logger log = LoggerFactory.getLogger(MemberTreeManageAction.class);

    public MemberTreeManageAction() {

    }
    /**
     * 删除内容,删除条件,1:包含mid列表,并且是在自己管理的栏目中
     *
     * @throws com.jspx.sober.exception.ValidException v
     * @throws Exception                               e
     */
    @Operate(caption = "保存")
    public void save() throws Exception {
        long uid = getLong("uid");
        if (uid < 1) {
            addFieldInfo(Environment.warningInfo, language.getLang(LanguageRes.needSelect));
            return;
        }
        String[] nodeIdArray = getArray("nodeId", true);
        nodeIdArray = treeItemDAO.addLimb(nodeIdArray);
        try {
            memberTreeDAO.deleteForUid(uid);
            if (!StringUtil.isEmpty(nodeId)) {
                for (String aCheckbox : nodeIdArray) {
                    if (StringUtil.isNULL(aCheckbox)) continue;
                    MemberTree memberTree = new MemberTree();
                    memberTree.setNodeId(aCheckbox);
                    memberTree.setUid(uid);
                    memberTree.setNamespace(treeItemDAO.getNamespace());
                    IUserSession userSession = (IUserSession) getUserSession();
                    if (userSession != null) {
                        memberTree.setPutName(userSession.getName());
                        memberTree.setPutUid(userSession.getUid());
                    }
                    if (memberTreeDAO.save(memberTree) > 0)
                        setActionResult(SUCCESS);
                }
                addActionMessage(language.getLang(LanguageRes.saveSuccess));
            } else {
                addActionMessage(language.getLang(LanguageRes.cleanAll));
            }
        } catch (Exception e) {
            log.error(ArrayUtil.toString(nodeIdArray,StringUtil.COMMAS),e);
            addActionMessage(language.getLang(LanguageRes.saveFailure));
            setActionResult(ERROR);
        }
    }

    public String execute() throws Exception {
        if (isMethodInvoked()) {
            memberTreeDAO.evict(MemberTree.class);
            memberTreeDAO.flush();
        }
        put("member", memberTreeDAO.load(Member.class, uid));
        return super.execute();
    }
}